 public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) { val = x; }
 }

class Solution01 {
	private TreeNode preNode;
	private TreeNode resNode;

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
		if(root == null || p == null){
			return null;
		}
		dfs(root, p);
		return resNode;
    }

	private void dfs(TreeNode root, TreeNode p){
		if(root != null){
			dfs(root.left, p);
			if(preNode != null && preNode.val == p.val){
				// 当前遍历节点作为后继
				resNode = root;
			}
			preNode = root;
			dfs(root.right, p);
		}
	}
}
